package com.next

class Issue
{
    String serialNumber

    String subject
    String description

    IssueStatus status
    IssueOrigin origin
    IssuePriority priority
    IssueType type
    IssueReason reason

    Account account
    Contact contact

    User createBy
    User modifyBy
    User user

    Asset asset

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static hasMany = [attachments: Attachment, activities: Activity, users: IssueTeam]

    static constraints = {
        serialNumber maxSize: 128, nullable: true, blank: true
        subject maxSize: 128
        description maxSize: 512

        asset nullable: true, blank: true

        account nullable: true, blank: true
        contact nullable: true, blank: true

        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        user nullable: true, blank: true
        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
    }

    String toString()
    {
        subject
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
        if (!user)
        {
            user = createBy
        }

        String charset = (('A'..'Z') + ('0'..'9')).join()
        String s1 = org.apache.commons.lang.RandomStringUtils.random(3, charset)
        String s2 = org.apache.commons.lang.RandomStringUtils.random(3, charset)
        String s3 = org.apache.commons.lang.RandomStringUtils.random(3, charset)
        serialNumber = "${s1}-${s2}-${s3}"


        // 初始化 所有者 反馈权限
        IssueTeam issueTeam = new IssueTeam(issue: this, user: user, teamRole: TeamRole.findByName('Edit'),
                                            contactPermission: TeamRole.findByName('Edit'),
                                            activityPermission: TeamRole.findByName('Edit'),
                                            teamPermission: TeamRole.findByName('Edit'),
                                            attachmentPermission: TeamRole.findByName('Edit'),
                                            notePermission: TeamRole.findByName('Edit'),
                                            flexFieldPermission: TeamRole.findByName('Edit'),
                                            addressPermission: TeamRole.findByName('Edit'))
        this.addToUsers(issueTeam)

        // 初始化 上级用户 反馈权限
        if (user.shareIssueToSupervisor)
        {
            User reportTo = user.reportTo
            while (reportTo)
            {
                IssueTeam issueTeam1 = new IssueTeam(issue: this, user: reportTo, teamRole: TeamRole.findByName('Read'),
                                                     contactPermission: TeamRole.findByName('Read'),
                                                     activityPermission: TeamRole.findByName('Read'),
                                                     teamPermission: TeamRole.findByName('Read'),
                                                     attachmentPermission: TeamRole.findByName('Read'),
                                                     notePermission: TeamRole.findByName('Read'),
                                                     flexFieldPermission: TeamRole.findByName('Read'),
                                                     addressPermission: TeamRole.findByName('Read'))
                this.addToUsers(issueTeam1)

                reportTo = reportTo.reportTo
            }
        }
    }

    def afterInsert()
    {
        def flexFieldCategories = FlexFieldCategory.findAllByObjectType('Issue')
        flexFieldCategories?.each {
            if (it?.condition)
            {
                def result = false
                try
                {
                    def shell = new GroovyShell()
                    def closure = shell.evaluate(it?.condition?.script)
                    result = closure(this)
                }
                catch (Exception e)
                {
                    log.error e
                }
                if (result)
                {
                    initFlexField(it)
                }
            }
            else
            {
                initFlexField(it)
            }
        }

    }

    def initFlexField(FlexFieldCategory flexFieldCategory)
    {
        def fields = FlexField.findAllByCategory(flexFieldCategory)
        fields?.each {
            def field = new FlexFieldInstance(category: flexFieldCategory, field: it, issue: this)
            field.save()
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
